//package com.rk.controller;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.rk.utils.UrlConnection;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.time.LocalDateTime;
//import java.util.Map;
//
///**
// * @Description: TODO
// * @Date : 2020/7/3
// * @Company : TODO
// * @Author : LeiXin
// */
//@Slf4j
//@RestController
//@RequestMapping("/webhook")
//public class WebHookController {
//
//    @Value("${rk.weixin.webhook.url}")
//    private String wxWebhookUrl;
//    @Value("${rk.weixin.webhook.status}")
//    private Boolean wxWebhookStatus;
//
//    @PostMapping("/notify")
//    public String notifyData(@RequestBody Map<String, Object> params) {
//
//        if (wxWebhookStatus) {
//            String objectKind = params.get("object_kind").toString();
//            String msg = null;
//            switch (objectKind) {
//                case "push":
//                    msg = pushContent(params);
//                    return UrlConnection.postMarkdown(wxWebhookUrl, msg);
//                case "pipeline":
//                    JSONObject objectAttributes = JSONObject.parseObject(params.get("object_attributes").toString());
//                    if ("failed".equals(objectAttributes.get("detailed_status").toString())) {
//                        return UrlConnection.postMarkdown(wxWebhookUrl, msg);
//                    }
//                    break;
//                default:
//                    throw new RuntimeException("回调失败");
//            }
//        }
//        return "success";
//    }
//
////    public static void main(String[] args) {
////        String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=721043d9-d434-4d1a-ab29-dc875bcfa72b";
////
//////        String pushStr = "{\"user_avatar\":\"https://git.querycap.com/uploads/-/system/user/avatar/235/avatar.png\",\"user_email\":\"leixin_javajob@126.com\",\"total_commits_count\":2,\"before\":\"041ccca0a47d18fab1cd48199151deb517efd7c0\",\"user_name\":\"雷鑫\",\"user_username\":\"1191265484251922433\",\"push_options\":{},\"checkout_sha\":\"a5565e16cf625ba7846a03a865607c16ef27f083\",\"project\":{\"path_with_namespace\":\"operation_sensing_platform/city-material\",\"ssh_url\":\"git@git.querycap.com:operation_sensing_platform/city-material.git\",\"description\":\"物料管理\",\"git_http_url\":\"https://git.querycap.com/operation_sensing_platform/city-material.git\",\"git_ssh_url\":\"git@git.querycap.com:operation_sensing_platform/city-material.git\",\"url\":\"git@git.querycap.com:operation_sensing_platform/city-material.git\",\"http_url\":\"https://git.querycap.com/operation_sensing_platform/city-material.git\",\"web_url\":\"https://git.querycap.com/operation_sensing_platform/city-material\",\"name\":\"city-material\",\"namespace\":\"operation_sensing_platform\",\"visibility_level\":0,\"default_branch\":\"master\",\"id\":1810,\"homepage\":\"https://git.querycap.com/operation_sensing_platform/city-material\"},\"repository\":{\"name\":\"city-material\",\"description\":\"物料管理\",\"visibility_level\":0,\"git_http_url\":\"https://git.querycap.com/operation_sensing_platform/city-material.git\",\"url\":\"git@git.querycap.com:operation_sensing_platform/city-material.git\",\"git_ssh_url\":\"git@git.querycap.com:operation_sensing_platform/city-material.git\",\"homepage\":\"https://git.querycap.com/operation_sensing_platform/city-material\"},\"object_kind\":\"push\",\"ref\":\"refs/heads/master\",\"user_id\":235,\"project_id\":1810,\"event_name\":\"push\",\"commits\":[{\"removed\":[],\"added\":[],\"author\":{\"name\":\"leixin\",\"email\":\"leixin_javajob@126.com\"},\"modified\":[\"src/main/java/com/rk/city/material/controller/WebHookController.java\"],\"id\":\"6351069ae8cff50c75e985880a6b8c1222a517c9\",\"message\":\"测试webhook11111\\n\",\"title\":\"测试webhook11111\",\"url\":\"https://git.querycap.com/operation_sensing_platform/city-material/-/commit/6351069ae8cff50c75e985880a6b8c1222a517c9\",\"timestamp\":\"2020-07-07T16:24:04+08:00\"},{\"removed\":[],\"added\":[],\"author\":{\"name\":\"leixin\",\"email\":\"leixin_javajob@126.com\"},\"modified\":[\"src/main/java/com/rk/city/material/controller/WebHookController.java\"],\"id\":\"a5565e16cf625ba7846a03a865607c16ef27f083\",\"message\":\"测试webhook22222\\n\",\"title\":\"测试webhook22222\",\"url\":\"https://git.querycap.com/operation_sensing_platform/city-material/-/commit/a5565e16cf625ba7846a03a865607c16ef27f083\",\"timestamp\":\"2020-07-07T16:24:28+08:00\"}],\"after\":\"a5565e16cf625ba7846a03a865607c16ef27f083\"}";
//////        JSONObject pushjsonObject = JSONObject.parseObject(pushStr);
//////        String p = new WebHookController().pushContent(pushjsonObject);
//////        System.out.println(p);
//////        String s = UrlConnection.postMarkdown(url, p);
//////        System.out.println(s);
////
////
////        String pStr = "{\"object_attributes\":{\"before_sha\":\"62638a144ff7e0b7b09feb5d91c26f6c449aa5ab\",\"detailed_status\":\"failed\",\"variables\":[],\"finished_at\":\"2020-07-07 05:56:21 UTC\",\"created_at\":\"2020-07-07 05:55:58 UTC\",\"source\":\"push\",\"sha\":\"041ccca0a47d18fab1cd48199151deb517efd7c0\",\"duration\":19,\"ref\":\"master\",\"stages\":[\"build\",\"ship\",\"deploy\"],\"id\":70943,\"tag\":false,\"status\":\"failed\"},\"commit\":{\"author\":{\"name\":\"leixin\",\"email\":\"leixin_javajob@126.com\"},\"id\":\"041ccca0a47d18fab1cd48199151deb517efd7c0\",\"message\":\"测试webhook\\n\",\"title\":\"测试webhook\",\"url\":\"https://git.querycap.com/operation_sensing_platform/city-material/-/commit/041ccca0a47d18fab1cd48199151deb517efd7c0\",\"timestamp\":\"2020-07-07T13:54:48+08:00\"},\"builds\":[{\"created_at\":\"2020-07-07 05:55:58 UTC\",\"manual\":false,\"when\":\"on_success\",\"allow_failure\":false,\"stage\":\"deploy\",\"artifacts_file\":{},\"name\":\"deploy\",\"id\":287369,\"user\":{\"avatar_url\":\"https://git.querycap.com/uploads/-/system/user/avatar/235/avatar.png\",\"name\":\"雷鑫\",\"email\":\"leixin_javajob@126.com\",\"username\":\"1191265484251922433\"},\"status\":\"skipped\"},{\"created_at\":\"2020-07-07 05:55:58 UTC\",\"manual\":false,\"when\":\"on_success\",\"allow_failure\":false,\"stage\":\"ship\",\"artifacts_file\":{},\"name\":\"ship\",\"id\":287368,\"user\":{\"avatar_url\":\"https://git.querycap.com/uploads/-/system/user/avatar/235/avatar.png\",\"name\":\"雷鑫\",\"email\":\"leixin_javajob@126.com\",\"username\":\"1191265484251922433\"},\"status\":\"skipped\"},{\"finished_at\":\"2020-07-07 05:56:20 UTC\",\"created_at\":\"2020-07-07 05:55:58 UTC\",\"manual\":false,\"when\":\"on_success\",\"allow_failure\":false,\"stage\":\"build\",\"artifacts_file\":{},\"name\":\"build\",\"started_at\":\"2020-07-07 05:56:01 UTC\",\"id\":287367,\"runner\":{\"is_shared\":false,\"description\":\"qingyun\",\"active\":true,\"id\":6},\"user\":{\"avatar_url\":\"https://git.querycap.com/uploads/-/system/user/avatar/235/avatar.png\",\"name\":\"雷鑫\",\"email\":\"leixin_javajob@126.com\",\"username\":\"1191265484251922433\"},\"status\":\"failed\"}],\"project\":{\"web_url\":\"https://git.querycap.com/operation_sensing_platform/city-material\",\"path_with_namespace\":\"operation_sensing_platform/city-material\",\"name\":\"city-material\",\"namespace\":\"operation_sensing_platform\",\"description\":\"物料管理\",\"visibility_level\":0,\"default_branch\":\"master\",\"id\":1810,\"git_http_url\":\"https://git.querycap.com/operation_sensing_platform/city-material.git\",\"git_ssh_url\":\"git@git.querycap.com:operation_sensing_platform/city-material.git\"},\"user\":{\"avatar_url\":\"https://git.querycap.com/uploads/-/system/user/avatar/235/avatar.png\",\"name\":\"雷鑫\",\"email\":\"leixin_javajob@126.com\",\"username\":\"1191265484251922433\"},\"object_kind\":\"pipeline\"}";
////        JSONObject pjsonObject = JSONObject.parseObject(pStr);
////        String pp = new WebHookController().pipelineContent(pjsonObject);
////        System.out.println(pp);
////        String sp = UrlConnection.postMarkdown(url, pp);
////        System.out.println(sp);
////    }
//
//    private String pushContent(Map<String, Object> params) {
//
//        // project(description)
//        // > event : push
//        // > discription : xxx
//        // > author : xxx
//        JSONObject project = JSONObject.parseObject(params.get("project").toString());
//
//        // 取最后一次提交
//        JSONArray commits = JSONArray.parseArray(params.get("commits").toString());
//        JSONObject commitObj = JSONObject.parseObject(commits.get(0).toString());
//        LocalDateTime time = null;
//        for (Object commit : commits) {
//            JSONObject curCommit = JSONObject.parseObject(commit.toString());
//            if (params.get("checkout_sha").toString().equals(curCommit.get("id").toString())) {
//                commitObj = curCommit;
//            }
////            LocalDateTime timestamp = LocalDateTime.parse(curCommit.get("timestamp").toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:sszzzz"));
////            if (time == null || timestamp.isAfter(time)) {
////                time = timestamp;
////                commitObj = curCommit;
////            }
//        }
//
//        StringBuffer msg = new StringBuffer("**" + project.get("name") + "**");
//        if (StringUtils.isNotBlank(project.get("description").toString())) {
//            msg.append("(").append(project.get("description").toString()).append(")");
//        }
//        msg.append("\n");
//        msg.append(">event : <font color=\"warning\">").append(params.get("object_kind")).append("</font>\n");
//        msg.append(">commit : ").append(commitObj.get("message")).append("\n");
//        msg.append(">author : ").append(JSONObject.parseObject(commitObj.get("author").toString()).get("name")).append("\n");
//        msg.append(">timestamp : ").append(commitObj.get("timestamp"));
//        return getMarkdownMsg(msg.toString()).toJSONString();
//    }
//
//    private String pipelineContent(Map<String, Object> params) {
//
//        // project(description)
//        // > event : pipeline
//        // > discription : xxx
//        // > author : xxx
//        JSONObject project = JSONObject.parseObject(params.get("project").toString());
//        JSONObject objectAttributes = JSONObject.parseObject(params.get("object_attributes").toString());
//
//        // 取最后一次提交
//        JSONObject commitObj = JSONObject.parseObject(params.get("commit").toString());
//
//        StringBuffer msg = new StringBuffer("**" + project.get("name") + "**");
//        if (StringUtils.isNotBlank(project.get("description").toString())) {
//            msg.append("(").append(project.get("description").toString()).append(")");
//        }
//        msg.append("\n");
//        msg.append(">event : <font color=\"warning\">").append(params.get("object_kind")).append("</font>\n");
//        msg.append(">status : <font color=\"info\">").append(objectAttributes.get("detailed_status")).append("</font>\n");
//        msg.append(">commit : ").append(commitObj.get("message")).append("\n");
//        msg.append(">author : ").append(JSONObject.parseObject(commitObj.get("author").toString()).get("name")).append("\n");
//        msg.append(">timestamp : ").append(commitObj.get("timestamp"));
//        return getMarkdownMsg(msg.toString()).toJSONString();
//    }
//
//    private JSONObject getMarkdownMsg(String markdownStr) {
//
//        JSONObject markdownJson = new JSONObject();
//        markdownJson.put("content", markdownStr);
//
//        JSONObject resultStrJson = new JSONObject();
//        resultStrJson.put("msgtype", "markdown");
//        resultStrJson.put("markdown", markdownJson);
//        return resultStrJson;
//    }
//
//}
